package com.landeb.shop.huo;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.landeb.shop.huo.act.GoodDetailActivity;
import com.landeb.shop.huo.act.Html5Activity;
import com.landeb.shop.huo.act.MainActivity;
import com.landeb.shop.huo.act.PayActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by blade on 2016/12/1.
 */

public class Json2Act {

//    private static String mJson;
//    private static Context mContext;
//    private static Json2Act mJson2Act;
//
//    private Json2Act(String json, Context context) {
//        mJson = json;
//        mContext = context;
//    }
//
//    public static Json2Act getJson2Act(String json, Context context) {
//
//        if (mJson2Act == null) {
//
//            mJson2Act = new Json2Act(json, context);
//
//        }
//
//        return mJson2Act;
//    }


    public static void makeJson2Act(String json, Context context) {


        try {
            JSONObject object = new JSONObject(json);

            String act = object.getString("act");

            switch (act) {
                case "web":

                    String title = getJsonValue(object, "title");
                    String url = getJsonValue(object, "url");

                    Html5Activity.toAct(context, title, url);

                    break;

                case "goods_detail":


                    String goodId = getJsonValue(object, "goods_id");

                    GoodDetailActivity.toAct(context, goodId);

                    break;

                case "cart":
                    MainActivity.toAct(context, MainActivity.CART_PAGE);
                    break;

                case "toast":

                    String info = getJsonValue(object, "message");

                    Toast.makeText(context, info, Toast.LENGTH_SHORT).show();

                    break;

                case "goods_cate":

                    String cid = getJsonValue(object, "id");

                    Log.e("makeJson2Act: ", cid);

                    if (context instanceof MainActivity){

                        Log.e( "makeJson2Act: ", "true");

                        ((MainActivity) context).goPage(MainActivity.PRODUCT_PAGE);
                        ((MainActivity) context).productsFragment.setListSelection(cid);

                    }

//                    if (cid.equals("10")) {

//                        MainActivity.toFragWithId(context, MainActivity.PRODUCT_PAGE, cid);


//                    } else {
//                        Skip4ProductFragActivity.toAct(context, cid);
//                    }

                    break;

                case "cashier":

                    String payId = getJsonValue(object, "id");

                    PayActivity.toAct(context,payId);

                    break;

            }


        } catch (Exception e) {
            Log.e("json2Act: ", e.toString());
            e.printStackTrace();
        }


    }

    private static String getJsonValue(JSONObject object, String key) {

        String value = "";

        try {

            value = object.getString(key);

        } catch (JSONException e) {
        }

        return value;
    }

}
